/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import dao.DaoAdministrador;
import dao.DaoUsuario;
import entidades.Administrador;
import entidades.Usuario;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author a968340
 */
@WebServlet(name = "ServletLogin", urlPatterns = {"/ServletLogin"})
public class ServletLogin extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String login = request.getParameter("login");
        String senha = request.getParameter("senha");
        if (login.equals("admin") && senha.equals("admin")) {
            RequestDispatcher rd = request.getRequestDispatcher("cadAdmin.jsp");
            rd.forward(request, response);
        } else {
            Usuario usuario = new Usuario(login, senha);
            List<Usuario> listUser = new DaoUsuario().buscaLoginSenha(login, senha);
            if (listUser.size() == 1) {
                Usuario u = listUser.get(0);
                HttpSession session = request.getSession();
                session.setAttribute("usuario", u);
                RequestDispatcher rd = request.getRequestDispatcher("perfil.jsp");
                rd.forward(request, response);
            } else {
                Administrador admin = new Administrador(login, senha);
                List<Administrador> listAdmin = new DaoAdministrador().buscaLoginSenha(login, senha);
                if (listAdmin.size() == 1) {
                    Administrador a = listAdmin.get(0);
                    HttpSession session = request.getSession();
                    session.setAttribute("administrador", a);
                    RequestDispatcher rd = request.getRequestDispatcher("homeAdmin.jsp");
                    rd.forward(request, response);
                } else {
                    request.setAttribute("erro", "Login ou senha estão errados");
                    RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                    rd.forward(request, response);
                }
            }
            //TODO: verificar se login e senha batem 
            //TODO: redirecionar para tela incial

            //TODO: verifica se está correto
            //TODO: se correto: cria sessão e redireciona para home
            //TODO: se não cria um parâmetro de erro e redireciona pra tela de login novamente
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
